package com.fsnbigdata.util;

import freemarker.template.TemplateException;

import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.*;
import java.util.Map;

public class WordUtils {
    private Configuration configuration = null;

    public WordUtils(){
        configuration = new Configuration();
        configuration.setDefaultEncoding("UTF-8");
    }

    public void createWord(Map<String, Object> dataMap, String url){
        configuration.setClassForTemplateLoading(this.getClass(), "/");  //FTL文件所存在的位置
        Template t=null;
        try {
            t = configuration.getTemplate( "model/file3405.ftl" ); //文件名
        } catch (IOException e) {
            e.printStackTrace();
        }
        File outFile = new File(url);  //导出文档的存放位置
        Writer out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }

        try {
            t.process(dataMap, out);
            out.close();
            configuration.clone();
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
